#-*- coding: UTF-8 -*-
import os, sys
import time
import logging 
import unittest
import logging.handlers

base_dir = os.path.dirname(os.path.dirname(__file__))
base_dir = str(base_dir)
base_dir = base_dir.replace('\\', '/')
base = base_dir.split('/test_case')[0]
file_path = base + "/logs/"
cut_time = time.strftime("%Y%m%d-%H%M%S", time.localtime())
file_name_a = os.path.join(file_path + cut_time + '.log')

class Logger(logging.Logger):
    def __init__(self, filename=None):
        super(Logger, self).__init__(self)
        # 日志文件名
        if filename is None:
            filename = file_name_a
        self.filename = filename

        # 创建一个handler，用于写入日志文件 (每天生成1个，保留10天的日志)
        fh = logging.handlers.TimedRotatingFileHandler(self.filename, 'D', 1, 10)
        fh.suffix = "%Y%m%d-%H%M.log"
        fh.setLevel(logging.DEBUG)


        # 再创建一个handler，用于输出到控制台 
        ch = logging.StreamHandler() 
        ch.setLevel(logging.WARNING) 

        # 定义handler的输出格式 
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s[line:%(lineno)d] - %(message)s') 
        fh.setFormatter(formatter) 
        ch.setFormatter(formatter) 

        # 给logger添加handler 
        #ch 控制台日志
        #fh 指定filename日志
        self.addHandler(fh)
        self.addHandler(ch)

if __name__ == "__main__":
    unittest.main()
    #loger1 = Logger()
    #loger1.debug("degbug log")
    #loger1.error("error log")


"""
base_dir = os.path.dirname(os.path.dirname(__file__))
base_dir = str(base_dir)
base_dir = base_dir.replace('\\', '/')
base = base_dir.split('/test_case')[0]
file_path = base + "/logs/"
cut_time = time.strftime("%Y%m%d-%H%M%S", time.localtime())
file_name_a = os.path.join(file_path + cut_time + '.log')

"""